package com.allwees.sender.sms;

import com.allwees.bs.core.modelbase.validate.ValidateException;
import com.allwees.core.cache.RedisUtil;

/**
 * Sms验证码验证器
 *
 * @author yangylsky
 * @since 2020-01-03
 *
 */
public interface SmsCodeVerifier {
	/**
	 * 虚拟手机号，测试用
	 */
	String[] MOCK_MOBILES = {"13800001111", "13800002222", "13800003333", "13800004444"};

	/**
	 * 虚拟手机号对应的验证码，测试用
	 */
	String MOCK_SMSCODE = "000000";

	/**
	 * 超级验证码，测试用
	 */
	String SUPER_SMSCODE = "147258";

	default void verify(String mobile, String code) {
		verify("", mobile, code);
	}

	default void verify(SenderBsType bsType, String mobile, String code) {
		verify(bsType.name(), mobile, code);
	}

	default void verify(String bsType, String mobile, String code) {
		if(SUPER_SMSCODE.equals(code)) {
			return;
		}
		String key = SmsSender.getVerifyCodeKey(bsType, mobile);
		if(!code.equalsIgnoreCase(RedisUtil.get(key))) {
			throw new ValidateException("验证码不正确或已过期");
		}
	}
}
